package com.sweets.look.ui.setting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sweet.satellite.GlobalConfig;
import com.sweet.satellite.TranslucentSBActivity;
import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.widget.MarkRadioDialog;
import com.sweets.look.util.ViewUtils;

/**
 * 浏览
 */
public class DisplaySettings extends TranslucentSBActivity {
    private CheckBox mCbColorMode;
    private CheckBox mCbHideUnderline;
    private CheckBox mCbOverViewMode;
    private CheckBox mCbShowAnimation;
    private CheckBox mCbTextReflow;
    private CheckBox mCbWakeLock;
    private CheckBox mCbWebviewPort;
    private Context mContext;
    private PreferenceManager mPreferences;
    private TextView mTextSize;
    private TextView mUrlBoxText;


    private void initColorModePreference() {
        this.mCbColorMode = (CheckBox) this.findViewById(R.id.cbColorMode);
        this.mCbColorMode.setChecked(this.mPreferences.getColorModeEnabled());
        this.mCbColorMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setColorModeEnabled(isChecked);
            }
        });
    }

    private void initHideUnderlinePreference() {
        this.mCbHideUnderline = (CheckBox) this.findViewById(R.id.cbHideUnderline);
        this.mCbHideUnderline.setChecked(this.mPreferences.getHideUnderlineEnabled());
        this.mCbHideUnderline.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setHideUnderlineEnabled(isChecked);
            }
        });
    }

    private void initOverViewModePreference() {
        this.mCbOverViewMode = (CheckBox) this.findViewById(R.id.cbOverViewMode);
        this.mCbOverViewMode.setChecked(this.mPreferences.getOverviewModeEnabled());
        this.mCbOverViewMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setOverviewModeEnabled(isChecked);
            }
        });
    }

    private void initShowAnimationPreference() {
        this.mCbShowAnimation = (CheckBox) this.findViewById(R.id.cbShowAnimation);
        this.mCbShowAnimation.setChecked(this.mPreferences.getAnimEnabled());
        this.mCbShowAnimation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setAnimEnabled(isChecked);
            }
        });
    }

    private void initTextReflowPreference() {
        this.mCbTextReflow = (CheckBox) this.findViewById(R.id.cbTextReflow);
        this.mCbTextReflow.setChecked(this.mPreferences.getTextReflowEnabled());
        this.mCbTextReflow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setTextReflowEnabled(isChecked);
            }
        });
    }

    private void initTextView() {
        switch (this.mPreferences.getTextSize()) {
            case 1: {
                this.mTextSize.setText(this.getResources().getString(R.string.size_largest));
                break;
            }
            case 2: {
                this.mTextSize.setText(this.getResources().getString(R.string.size_large));
                break;
            }
            case 3: {
                this.mTextSize.setText(this.getResources().getString(R.string.size_normal));
                break;
            }
            case 4: {
                this.mTextSize.setText(this.getResources().getString(R.string.size_small));
                break;
            }
            case 5: {
                this.mTextSize.setText(this.getResources().getString(R.string.size_smallest));
                break;
            }
        }

        switch (this.mPreferences.getUrlBoxChoice()) {
            case 0: {
                this.mUrlBoxText.setText(this.getResources().getString(R.string.url_box_title));
                break;
            }
            case 1: {
                this.mUrlBoxText.setText(this.getResources().getString(R.string.url_box_url));
                break;
            }
            case 2: {
                this.mUrlBoxText.setText(this.getResources().getString(R.string.url_box_domain));
                break;
            }
        }
    }

    private void initWakeLockPreference() {
        this.mCbWakeLock = (CheckBox) this.findViewById(R.id.cbWakeLock);
        this.mCbWakeLock.setChecked(this.mPreferences.getWakeLockEnabled());
        this.mCbWakeLock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setWakeLockEnabled(isChecked);
            }
        });
    }

    private void initWebviewPortPreference() {
        this.mCbWebviewPort = (CheckBox) this.findViewById(R.id.cbWebviewPort);
        this.mCbWebviewPort.setChecked(this.mPreferences.getUseWideViewportEnabled());
        this.mCbWebviewPort.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DisplaySettings.this.mPreferences.setUseWideViewportEnabled(isChecked);
            }
        });
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.settings_display);
        this.setToolbarTitle(R.string.settings_display);

        this.mPreferences = PreferenceManager.getInstance();
        this.mContext = this;
        this.mTextSize = (TextView) this.findViewById(R.id.webviewTextSize);
        this.mUrlBoxText = (TextView) this.findViewById(R.id.urlBoxText);
        this.initTextView();
        this.initTextReflowPreference();
        this.initWebviewPortPreference();
        this.initOverViewModePreference();
        this.initHideUnderlinePreference();
        this.initColorModePreference();
        this.initShowAnimationPreference();
        this.initWakeLockPreference();
        ViewUtils.swipeToFinish(this.findViewById(R.id.settingLayout), this);
    }

    public void onSettingsItemClick(View v) {
        CheckBox v2;
        boolean v0 = true;
        switch (v.getId()) {
            case R.id.layoutTextSize: {
                new MarkRadioDialog(this.mContext).builder()
                        .setTitle(this.getResources().getString(R.string.title_text_size))
                        .setCancelable(true).setSingleChoiceItems(R.array.array_text_size,
                        this.mPreferences.getTextSize() - 1, new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                DisplaySettings.this.mPreferences.setTextSize(checkedId + 1);
                                DisplaySettings.this.initTextView();
                            }
                        }).show();
                break;
            }
            case R.id.layoutTextReflow: {
                v2 = this.mCbTextReflow;
                if (this.mCbTextReflow.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutWebviewPort: {
                v2 = this.mCbWebviewPort;
                if (this.mCbWebviewPort.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutOverViewMode: {
                v2 = this.mCbOverViewMode;
                if (this.mCbOverViewMode.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutHideUnderline: {
                v2 = this.mCbHideUnderline;
                if (this.mCbHideUnderline.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutUrlBox: {
                new MarkRadioDialog(this.mContext).builder()
                        .setTitle(this.getResources().getString(R.string.url_box))
                        .setCancelable(true).setSingleChoiceItems(R.array.array_url_box,
                        this.mPreferences.getUrlBoxChoice(), new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                DisplaySettings.this.mPreferences.setUrlBoxChoice(checkedId);
                                DisplaySettings.this.initTextView();
                            }
                        }).show();
                break;
            }
            case R.id.layoutColorMode: {
                v2 = this.mCbColorMode;
                if (this.mCbColorMode.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutShowAnimation: {
                v2 = this.mCbShowAnimation;
                if (this.mCbShowAnimation.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutWakeLock: {
                v2 = this.mCbWakeLock;
                if (this.mCbWakeLock.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
        }
    }


}

